package com.sky.renren.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.renren.api.connect.android.PasswordFlowRequestParam;
import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.exception.RenrenAuthError;
import com.renren.api.connect.android.view.RenrenAuthListener;
import com.sky.renren.R;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements OnClickListener, RenrenAuthListener{
	
//	private static final String TAG = "LoginActivity";
	
	private static final String API_KEY = "6b1016db20c540e78bd1b20be4c707a3";
	private static final String SECRET_KEY = "4723a695c09e4ddebbe8d87393d95fb4";
	private static final String APP_ID = "105381";
	
	private Renren renren;
	
	private EditText usernameText = null;
	private EditText passwordText = null;
	private Button loginButton = null;
	private ProgressDialog dialog = null;
	
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		usernameText = (EditText)findViewById(R.id.loginUsernameText);
		passwordText = (EditText)findViewById(R.id.loginPasswordText);
		loginButton = (Button)findViewById(R.id.loginButton);
		loginButton.setOnClickListener(this);
//		InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);  
//		 imm.showSoftInput(usernameText, 0);
		handler = new Handler();
		renren = new Renren(API_KEY, SECRET_KEY, APP_ID, this);
		if (!TextUtils.isEmpty(usernameText.getText())) {
			usernameText.selectAll();
		}
		init();
	}
	
	private void init() {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				InputMethodManager imm = (InputMethodManager) LoginActivity.this
						.getSystemService(INPUT_METHOD_SERVICE);
//				imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				 imm.showSoftInput(usernameText, 0);
			}
		}, 500);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.loginButton) {
			String username = usernameText.getText().toString().trim();
			String password = passwordText.getText().toString().trim();
			if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
				Toast.makeText(this, R.string.login_null_error, Toast.LENGTH_SHORT).show();
			} else {
				dialog = new ProgressDialog(this);
				dialog.setMessage(getResources().getString(R.string.logining));
				dialog.show();
				PasswordFlowRequestParam param = new PasswordFlowRequestParam(username, password);
				renren.authorize(LoginActivity.this, param , this);
			}
		}
	}

	@Override
	public void onCancelAuth(Bundle arg0) {
		
	}

	@Override
	public void onCancelLogin() {
		
	}

	@Override
	public void onComplete(Bundle arg0) {
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				dialog.dismiss();
				Toast.makeText(LoginActivity.this, "login success", Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	public void onRenrenAuthError(RenrenAuthError arg0) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				dialog.dismiss();
				usernameText.requestFocus();
				Toast.makeText(LoginActivity.this, "login error",Toast.LENGTH_SHORT).show();
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (renren != null) {
			renren.authorizeCallback(requestCode, resultCode, data);
		}
	}
	
}
